package com.xf.lastmall.util;


import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * ClassName:OrderNoGenUtil <br/>
 *
 * @see
 */
@Component
public class OrderNoGenUtil {
    private static final String ORDER_NO_KEY_PREFIX = "system:orderno:increment:";
    private static final String ORDERSUBNO_KEY_PREFIX = "system:ordersubno:increment:";
    private static final String ORDER_REFUND_BATCH_NO = "system:refundbatchno:increment";

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * getNextOrderNo:生成一个新的不重复的订单编号. <br/>
     * 注意:如果一个商场的一天的自增序号大于6位数。将影响到最终订单号的长度。.<br/>
     *
     * @param mallSeq CS :星沙
     * @return orderNo:CS 160905 00007064
     * @author william.zuo
     */
    public String getNextOrderNo(String mallSeq) {
        Result result = getResult(mallSeq, ORDER_NO_KEY_PREFIX);


        // 混淆编码
        int r1 = (int) (Math.random() * 10);
        int r2 = (int) (Math.random() * 10);

        return mallSeq + result.timeStr() + result.hexStr().substring(0, 4) +
                r1 + result.hexStr().charAt(4) +
                r2 + result.hexStr().substring(5);
    }

    private Result getResult(String prefix, String mallSeq) {
        LocalDateTime dateTime = LocalDateTime.now();
        String timeStr = dateTime.format(DateTimeFormatter.ofPattern("yyMMdd"));
        String key = prefix + mallSeq + timeStr;
        Long increment = stringRedisTemplate.opsForValue().increment(key);
        assert increment != null;
        if (increment.intValue() == 1) {
            stringRedisTemplate.expire(key, Duration.ofDays(1));
        }
        String hexStr = increment + "";
        if (increment > 999999) {
            increment += 9485761;//A00000
            hexStr = Long.toHexString(increment).toUpperCase();
        }
        hexStr = "0".repeat(6 - hexStr.length()) + hexStr;
        return new Result(timeStr, hexStr);
    }

    private record Result(String timeStr, String hexStr) {
    }

    public String getNextOrderSubNo(String mallSeq, String orderType) {

        Result result = getResult(mallSeq, ORDERSUBNO_KEY_PREFIX);

        // 混淆编码
        int r1 = (int) (Math.random() * 10);
        int r2 = (int) (Math.random() * 10);

        return mallSeq + orderType + result.timeStr() + result.hexStr().substring(0, 4) +
                r1 + result.hexStr().charAt(4) +
                r2 + result.hexStr().substring(5);
    }

    public String getNextBatchNo(String mallSeq) {


        LocalDateTime dateTime = LocalDateTime.now();
        String timeStr = dateTime.format(DateTimeFormatter.ofPattern("yyMMdd"));
        String key = ORDER_REFUND_BATCH_NO + mallSeq + timeStr;
        Long increment = stringRedisTemplate.opsForValue().increment(key);
        assert increment != null;
        if (increment.intValue() == 1) {
            stringRedisTemplate.expire(key, Duration.ofDays(1));
        }

        //同一个场一天退款次数  不超过10万+323310
        String hexStr = increment + "";
        if (increment > 99999) {
            increment += 555360;//A0000
            hexStr = Long.toHexString(increment).toUpperCase();
        }

        hexStr = "0".repeat(6 - hexStr.length()) + hexStr;

        return timeStr + mallSeq + hexStr + (int) (Math.random() * 10) + (int) (Math.random() * 10);
    }


}
